/**
 * Created by guosc on 2016/10/25 0025.
 */
define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule',
    'framework/groupDataSource/GroupDataSourceModule'
],function (angular, JSONPrettyFormat) {
    var AddUserToGroupModule = angular.module("AddUserToGroupModule", [
        'DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule',
        'GroupDataSourceModule']);

    AddUserToGroupModule.controller("SysGroupController", function ($scope,Resource,$location,GillionMsg,GillionMsgService,$http) {
        var groupUserRoles=Resource("/grule-manager/groupUserRole");
        $scope.groupId= GillionMsgService.getInputData();
        $http({
            url:"/grule-manager/user/userName",
            method:"GET",
            params:{
                groupId:$scope.groupId
            }
        }).success(function (resp) {
            $scope.users=resp;
        });
        var checkRows = document.getElementsByName("checkbox");
        var checks=[];
        $scope.confirm=function(){
            for(var i=0;i<checkRows.length;i++){
                if(checkRows[i].checked){
                    checks.push(checkRows[i].value)
                }
            }
            if(checks.length==0){
                GillionMsg.alert("提示", "没有选择用户.");
            }else{
                var arr=[], row;
                for(var i=0;i<checks.length;i++){
                    row = {};
                    row.groupId = $scope.groupId;
                    row.userId = checks[i];
                    arr.push(row);
                }
                groupUserRoles.create(arr,function(response){
                    if (response.success) {
                        GillionMsgService.setOutputData({success: true});
                        GillionMsgService.close();
                    } else {
                        GillionMsg.alert("提示", "加入失败.");
                    }
                })
            }

        };
        $scope.cancel=function(){
            GillionMsgService.close();
        }
    });

});